package com.xinbochuang.machine.admin.service.impl.plan;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.plan.CheckRoomCabinet;
import com.xinbochuang.machine.admin.domain.vo.EquCabinetVo;
import com.xinbochuang.machine.admin.mapper.plan.CheckRoomCabinetMapper;
import com.xinbochuang.machine.admin.mapper.plan.EquCabinetVoMapper;
import com.xinbochuang.machine.admin.service.plan.ICheckRoomCabinetService;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄晓鹏
 * @date 2020-09-10
 */
@Service
public class CheckRoomCabinetServiceImpl extends ServiceImpl<CheckRoomCabinetMapper, CheckRoomCabinet> implements ICheckRoomCabinetService {


    @Resource
    private EquCabinetVoMapper cabinetVoMapper;
    @Override
    public TableDataInfo selectPlanCabinetListByApp(CheckRoomCabinet checkRoomCabinet) {
        //根据查询条件查询集合列表
        QueryWrapper<CheckRoomCabinet> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CheckRoomCabinet::getPlanId,checkRoomCabinet.getPlanId()).eq(CheckRoomCabinet::getRoomId,checkRoomCabinet.getRoomId());
        queryWrapper.lambda().orderByAsc(CheckRoomCabinet::getStatus);
        List<CheckRoomCabinet> planList;
        IPage<CheckRoomCabinet> list = new Page<>();
        if (checkRoomCabinet.getPageNum() == null) {
            planList = list(queryWrapper);
            list.setRecords(planList);
            list.setTotal(planList.size());
        } else {
            list = page(new Page<>(checkRoomCabinet.getPageNum(), checkRoomCabinet.getPageSize()), queryWrapper);
            planList = list.getRecords();
        }
        List<String> romeIds = new ArrayList<>();
        Map<String,String> roomMap=new HashMap<>();
        for (CheckRoomCabinet planRome : planList) {
            romeIds.add(planRome.getCabinetId());
        }
        List<EquCabinetVo> roomList = cabinetVoMapper.selectList(new QueryWrapper<EquCabinetVo>().in("uuid", romeIds));

        for (EquCabinetVo macRoomVo : roomList) {
            roomMap.put(macRoomVo.getUuid(),macRoomVo.getName());
        }

        for(CheckRoomCabinet checkPlanRoom1:planList){
            if(roomMap.containsKey(checkPlanRoom1.getCabinetId())){
                checkPlanRoom1.setName(roomMap.get(checkPlanRoom1.getCabinetId()));
            }
        }
        /**
         * 根据计划/机房对应，查询机房列表，放入计划中
         */
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.HTTP_OK);
        rspData.setMsg("查询成功");
        rspData.setRows(list.getRecords());
        rspData.setTotal(list.getTotal());
        return rspData;
    }
}
